layui.define(['jquery'],function(exports){
	var $ = layui.jquery;	
	const SERVICE_ADDRESS = "http://localhost:8181/"
	//const SERVICE_ADDRESS = "https://www.nongfeiguanjia.com/huishou-service/"
	var request = new function(){
		this.ajaxPost = function(url ,data, fn,options){
			var iniOptions = {
				url: SERVICE_ADDRESS+url,
				type: 'POST',
				data: data,
				dataType: 'json',
				contentType: "application/json;charset=UTF-8",
				headers:{
					"accessToken":sessionStorage.getItem('token')
				},
				success:function(res){
					if(res.code > 0){
						switch(res.code){
							case 3:
							case 4:sessionStorage.clear();
										window.location.href ="/login.html";
						}
					}
					fn(res);
				}
			}
			$.extend(iniOptions, options);
			$.ajax(iniOptions);
		}
		
		this.ajaxPostAlert = function(url ,data, fn,options){
			var iniOptions = {
				url: SERVICE_ADDRESS+url,
				type: 'POST',
				data: data,
				dataType: 'json',
				contentType: "application/json;charset=UTF-8",
				headers:{
					"accessToken":sessionStorage.getItem('token')
				},
				success:function(res){
					if(res.code > 0){
						layer.msg(res.msg, {
							offset: '50px',
							icon: 2,
							time: 2000
						})
						switch(res.code){
							case 3:
							case 4:sessionStorage.clear();
										window.location.href ="/login.html";
						}
					}else if(res.code == 0){
						layer.msg(res.msg, {
							offset: '50px',
							icon: 1,
							time: 2000
						})
					}
					fn(res);
				},
				error:function(){
					layer.msg("服务器繁忙,请稍后再试~", {
							offset: '50px',
							icon: 2,
							time: 2000
					})
				}
			};
			$.extend(iniOptions, options);
			$.ajax(iniOptions);
		}
		
		this.post = async function(url ,data){
			return fetch(SERVICE_ADDRESS+url,{
					method: 'POST',
					headers:{
						"accessToken":sessionStorage.getItem('token'),
						'Accept': 'application/json, text/plain, */*',
                    	'Content-Type': 'application/json'
					},
					body:JSON.stringify(data)
				}).then(response=>{
					if(response.status == 200){
						return response.json();
					}
					return Promise.reject(response)
				}).catch(err=>{
					return Promise.reject(err)
				}).then(res=>{
					if(res.code > 0){
						switch(res.code){
							case 3:
							case 4:sessionStorage.clear();
										window.location.href ="/login.html";
						}
						return Promise.reject(res)
					}else{
						return res
					}
				})
			
		}
		
		this.postAlert =async function(url ,data){
			return fetch(SERVICE_ADDRESS+url,{
					method: 'POST',
					headers:{
						"accessToken":sessionStorage.getItem('token'),
						'Content-Type': 'application/json;charset=UTF-8' ,
						"Accept":"application/json, text/plain, */*",
					},
					body:JSON.stringify(data)
				}).then(response=>{
					if(response.status == 200){
						return response.json();
					}
					return Promise.reject(response)
				}).catch((err)=>{
					layer.msg("服务器繁忙,请稍后再试~", {
							offset: '50px',
							icon: 2,
							time: 2000
						})
					return Promise.reject(err)
				}).then(res=>{
					if(res.code > 0){
						layer.msg(res.msg, {
							offset: '50px',
							icon: 2,
							time: 2000
						})
						switch(res.code){
							case 3:
							case 4:sessionStorage.clear();
										window.location.href ="/login.html";
						}
						return Promise.reject(res)
					}else if(res.code == 0){
						layer.msg(res.msg, {
							offset: '50px',
							icon: 1,
							time: 2000
						})
						return res
					}
				})
		}
	};
	
	exports("request",request);
})